<?php
/**
 * 获取主页内容
 */
namespace app\api\controller;
use app\api\model\CharitableClass;
use app\api\server\Token;
use app\common\model\Banner as BannerModel;
use app\api\model\CharitableClass as CharitableClassModel;
use app\api\model\CharitableArticle as CharitableArticleModel;
use app\api\model\Link as LinkModel;
use app\api\model\Member as MemberModel;
use app\api\model\BaseConfig as BaseConfigModel;
use app\common\model\Joinus as JoinusModel;
use think\facade\Cache;

class Index extends Baek
{

    /**
     * 首页
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function index(){
        $data['juzhen'] = $this->common->member_matrix($this->mid);
        $data['banner']    = BannerModel::position_list('index',$this->language);
        $data['charitable_class'] = CharitableClassModel::getList($this->language);
        $data['youqing_link'] = LinkModel::getList($this->language);
        $data['coin']   = MemberModel::wallet($this->mid);

        $data['team_num']   = 0;
        //团队人数
        $team = MemberModel::member_relationship($this->mid);
        if(!empty($team['all'])) $data['team_num'] = count($team['all']);
        //矩阵介绍信息
        $data['introduction'] = BaseConfigModel::config_data('matrix_introduction');
        //访问+1
        Cache::inc('browse',1);
        Cache::inc('browse'.date('Y'),1);
        return api_success($data,'',$this->language);
    }

    /**
     * 验证助记词
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function verification(){
        $member = MemberModel::getDetails($this->mid);
        if($member['type'] == 1){
            MemberModel::where('id',$this->mid)->update(['type'=>2]);
            //用户已存在,生成token
            $token = (new Token())->generateToken(['id'=>$this->mid,'type'=>2]);
            return api_success(['token'=>$token],'Verification succeeded',$this->language);
        }

        return api_error('','Verified',$this->language);
    }

    /**
     * 获取助记词
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function mnemonic(){
        $member = MemberModel::getDetails($this->mid);
        $mnemonic = explode(' ',$member['mnemonic']);
        return api_success($mnemonic);
    }

    /**
     * 慈善列表
     * @return \think\Response|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function charitable(){

        $id  = $this->request->post('id', '', 'intval');
        $limit  = $this->request->post('limit', 20, 'intval');
        $page = $this->request->post('page', 1, 'intval');
        //分类ID
        if(empty($id)) return api_error('','Missing parameter',$this->language);
        $class = CharitableClass::getDetails($id,$this->language);  //查询分类
        //判断是否有该分类
        if(empty($class)) return api_error('','Missing parameter',$this->language);

        $res = CharitableArticleModel::getApiList($id,$page,$limit,$this->language);
        $res['class'] = $class;

        return api_success($res,'',$this->language);
    }

    //慈善文章详情
    function charitable_article(){
        $id  = $this->request->post('id', '', 'intval');
        $res = CharitableArticleModel::apiGetDetails($id);

        if(empty($res)) return api_error('','No such article',$this->language);

        return api_success($res);
    }

    /**
     * 读取主钱包地址
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function address(){
        $address = [
            'usdt_address'  =>  [
                'name'  =>  'USDT链钱包地址',
                'content'   =>  BaseConfigModel::config_data('usdt_address')
            ],
            'usdt_contract'  =>  [
                'name'  =>  'USDT合约地址',
                'content'   =>  BaseConfigModel::config_data('usdt_contract')
            ],
            'lcc_address'  =>  [
                'name'  =>  'LCC链钱包地址',
                'content'   =>  BaseConfigModel::config_data('lcc_address')
            ],
            'lcc_contract'  =>  [
                'name'  =>  'LCC合约地址',
                'content'   =>  BaseConfigModel::config_data('lcc_contract')
            ],
        ];
        return api_success($address,'',$this->language);
    }

    /**
     * 加入我们
     * @return \think\Response|void
     */
    function joinus(){
        $data = $this->request->only(explode(',','name,mailbox,tel,corporate_name,corporate_tel,corporate_logo,corporate_address,introduction,photo'),'post',null);
        $id = JoinusModel::add($this->mid,$data['name'],$data['mailbox'],$data['tel'],$data['corporate_name'],$data['corporate_tel'],$data['corporate_logo'],$data['corporate_address'],$data['introduction'],$data['photo']);
        if(empty($id)) return api_error('','Submit failed',$this->language);
        return api_success();
    }

    /**
     * 获取系统文章
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function sys_article(){
        $id  = $this->request->post('id', '', 'intval');
        $name = 'ecology';
        switch ($id){
            case 1:
                $name = '4';  //生态
                break;
            case 2:
                $name = '5'; //玩法介绍
                break;
            case 3:
                $name = '6';    //关于MMST
                break;
            case 4:
                $name = '7';    //联系我们
                break;
        }
        //获取指定文章
        $content = CharitableArticleModel::getDetails($name)['content'];
        return api_success($content);
    }
}